<?php
class ControllerLocalisationLanguage extends AdminController {

	public function index() {
		M('localisation/language');
		$this->getList();
	}

	public function insert() {

		M('localisation/language');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_language->addLanguage($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/language'));
		}

		$this->getForm();
	}

	public function update() {

		M('localisation/language');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_language->editLanguage($this->request->get['language_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/language'));
		}

		$this->getForm();
	}

	public function delete() {

		M('localisation/language');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $language_id) {
				$this->model_localisation_language->deleteLanguage($language_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/language'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$language_total = $this->model_localisation_language->getTotalLanguages();

		$this->data['languages'] = $this->model_localisation_language->getLanguages($qf);

		foreach ($this->data['languages'] as $i => $result) {
			$this->data['languages'][$i]['name'] = $result['name'] . (($result['code'] == C('config_language')) ? $this->_['text_default'] : null);
		}

		$this->data['pagination'] = $this->pagination($language_total)->render();

		$this->display('localisation/language_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		if (isset($this->request->get['language_id'])) {
			$this->data['action'] = UA('localisation/language/update', 'language_id=' . $this->request->get['language_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_localisation_language->getLanguage($this->request->get['language_id']);
			}
		} else {
			$this->data['action'] = UA('localisation/language/insert');
		}
		
		$this->display('localisation/language_form.tpl');
	}

	protected function validateForm() {
		$pass = true;
		if (!range_length($this->request->post['name'], 3, 32)) {
			$this->setMessage('error_name', $this->_['error_name']);
			$pass = false;
		}

		if (utf8_strlen($this->request->post['code']) < 2) {
			$this->setMessage('error_code', $this->_['error_code']);
			$pass = false;
		}

		if (!$this->request->post['locale']) {
			$this->setMessage('error_locale', $this->_['error_locale']);
			$pass = false;
		}

		if (!$this->request->post['directory']) {
			$this->setMessage('error_directory', $this->_['error_directory']);
			$pass = false;
		}

		if (!$this->request->post['filename']) {
			$this->setMessage('error_filename', $this->_['error_filename']);
			$pass = false;
		}

		if (!range_length($this->request->post['image'], 3, 32)) {
			$this->setMessage('error_image', $this->_['error_image']);
			$pass = false;
		}

		return $pass;
	}

	protected function validateDelete() {

		M('setting/store');
		M('sale/order');

		foreach ($this->request->post['selected'] as $language_id) {
			$language_info = $this->model_localisation_language->getLanguage($language_id);

			if ($language_info) {
				if (C('config_language') == $language_info['code']) {
					$this->setMessage('error_warning', $this->_['error_default']);
					return false;
				}

				if (C('config_admin_language') == $language_info['code']) {
					$this->setMessage('error_warning', $this->_['error_admin']);
					return false;
				}

				$store_total = $this->model_setting_store->getTotalStoresByLanguage($language_info['code']);

				if ($store_total) {
					$this->setMessage('error_warning', sprintf($this->_['error_store'], $store_total));
					return false;
				}
			}

			$order_total = $this->model_sale_order->getTotalOrdersByLanguageId($language_id);

			if ($order_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_order'], $order_total));
				return false;
			}
		}

		return true;
	}
}
?>